package sample.ws.service;

import com.sun.xml.internal.ws.developer.JAXWSProperties;
import lombok.Setter;
import org.dom4j.DocumentException;
import org.springframework.util.StringUtils;
import sample.ws.entity.CommonRepository;
import sample.ws.entity.ServiceEntity;
import sample.ws.util.AccessComponent;
import sample.ws.util.IPUtil;
import sample.ws.util.LocalCacheUtils;
import sample.ws.util.XmlUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.spi.http.HttpExchange;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.time.LocalDateTime;
import java.util.List;

/**
 * DataServiceImpl..
 *
 * @author Lizhong
 * @date 2019/7/13
 */

@javax.jws.WebService(serviceName = "DataServiceImlp", portName = "HTTPEndpoint",
        targetNamespace = "http://www.ssii.com.cn/",
        endpointInterface = "sample.ws.service.DataService")
// @Service
@Setter
public class DataServiceImpl implements DataService {

    @Resource
    private IService iService;
    @Resource
    private AccessComponent accessComponent;
    @Resource
    private HttpServletRequest httpRequest;
    @Resource
    private WebServiceContext wsContext;

    @Resource
    CommonRepository commonRepository;


    @Override
    public String getMetaData(String name, String token, Long page, Long size) {

        long begin = System.currentTimeMillis();
        LocalDateTime start = LocalDateTime.now();

        // token校验
        ServiceEntity service = iService.findByName(name);
        Long id = service.getId();
        boolean access = accessComponent.hasAccessible(getClientIp(), id);
        if (!access) {
            // 操作过于频繁受限
            return "操作过于频繁";
        }

        List<Object[]> o = commonRepository.selectByTableNameAndAttr(id, page, size);
        String result = object2xml(service.getTableName(), id, o);

        LocalDateTime finish = LocalDateTime.now();
        long end = System.currentTimeMillis();
        return result;

    }

    private String object2xml(String table, Long id, List<Object[]> o) {
        List<String> columns = LocalCacheUtils.columnsCache.get(id);


        try {
            return XmlUtils.list2xml(table, columns, o);
        } catch (DocumentException e) {
            return e.getMessage();
        } catch (IOException e) {
            return e.getMessage();
        }

    }

    private String getClientIp() {

        try {
            InetSocketAddress isa =null;
           /* String ipAddress = IPUtil.getIpAddress(httpRequest);
            if (!StringUtils.isEmpty(ipAddress)) return ipAddress;*/

            MessageContext mc = wsContext.getMessageContext();
            HttpExchange exchange = (HttpExchange) mc
                    .get(JAXWSProperties.HTTP_EXCHANGE);
            if (exchange != null)
                isa = exchange.getRemoteAddress();
            if (!StringUtils.isEmpty(isa)) return isa.getAddress().toString();

            HttpServletRequest request = (HttpServletRequest)
                    (mc.get(MessageContext.SERVLET_REQUEST));
            return IPUtil.getIpAddress(request);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*public String method02() {
        People people = new People();
        people.setAge(12);
        People people0 = new People();
        people0.setAge(12);
        Response result = new Response();
        ;
        result.setPage(10);
        result.setResutl(new ArrayList<People>(Arrays.asList(people, people0)));
        XStream xstream = new XStream();
        String xml = xstream.toXML(result);
        System.out.println(xml);
        return xml;
    }*/

}
